﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Security.Cryptography;

namespace z {
	/// <summary>
	/// Summary description for WebService
	/// </summary>
	//[WebService(Namespace = "")]
	[WebServiceBinding]
	//[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	public class WebService : System.Web.Services.WebService {
		private void submitSes() {
			Session["Ses"] = Ses;
		}
		public z.c.ZSession Ses {
			get {
				return Session["Ses"] as c.ZSession;
			}
			set {
				Session["Ses"]=value;
			}
		}
	
		public WebService() {
		}
		[WebMethod(enableSession: true)]
		public m.StatusT getStatus(int Res) {
			return new m.StatusT(Ses,Res);
		}
		[WebMethod(enableSession: true)]
		public void login(string Acc, string Pass) {
			var phash = SHA256.Create();
			var hash0 = phash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Pass));
			var u = c.App.DB.Users.Where(x => x.Acc.Equals(Acc)).Single();
			int i=0;
			for (i = 0; i < 32; i++) {
				if(u.Pass[i]!=hash0[i]) break;
			}
			if (i==32) {
				Ses = new c.ZSession();
				Ses.User = u;
				submitSes();
			}
		}
		[WebMethod(enableSession: true)]
		public void registry(string Acc,string Pass) {
			var phash = SHA256.Create();
			var hash0 = phash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Pass));
			var u = new m.User();
			u.Acc = Acc;
			u.Pass = hash0;
			c.App.DB.Users.InsertOnSubmit(u);
			c.App.DB.SubmitChanges();
		}
		[WebMethod(enableSession: true)]
		public void newRoom() {
			Ses.Room = new c.Room();
		}
		[WebMethod(enableSession: true)]
		public void joinRoom(string ID) {
			int i;
			if(!int.TryParse(ID,out i))return;
			Ses.Room = c.App.Rooms.Where(x => x.ID == i).Single();
		}
		[WebMethod(enableSession: true)]
		public void choseGame() {
			//Ses.CurrentGame=c.App.Games.Where(x=>x.GetProperty("ID")==x
		}
	}
}
